﻿'******************************************************
'******************************************************
'Application Title: Google Widget Editor
'version: 1.0
'Date: 01-02-2008
'Requirements: win2k or later
'Author: Andrew Pye
'Author Email: StretchMasterP@googlemail.com
'License: Released under GNU GENERAL PUBLIC LICENSE v3
'ToDo:
'Changes:Imports System.Text
'******************************************************
'******************************************************

Imports System.Text

Public Class CodeFormatter
    Public Shared Function FormatCode(ByVal Code As String) As String
        Dim Strbuf As String = ""
        Dim TCode As String = Code.Trim
        Dim sb As New StringBuilder
        Dim Stack As New Stack(Of String)
        Dim NewLine As Boolean = False
        Dim LastItem As String = ""
        Dim OpenQuote As Boolean = False
        For i As Integer = 0 To TCode.Length - 1
            Strbuf = TCode.Substring(i, 1)

            If Strbuf = ControlChars.Quote Or Strbuf = "'" Then
                If OpenQuote = True Then
                    OpenQuote = False
                Else
                    OpenQuote = True
                End If
            End If

            If OpenQuote = False Then
                Select Case Strbuf
                    Case "<", "(", "{"
                        NewLine = False
                        Stack.Push(Strbuf)
                    Case ")"
                        Stack.Pop()
                        NewLine = False
                    Case ">", "}"
                        NewLine = True
                        Stack.Pop()
                        'Case " "
                        '    If Stack.Count = 0 Then
                        '        NewLine = True
                        '    End If
                End Select
                Select Case LastItem & Strbuf
                    Case "/*"
                        NewLine = False
                        Stack.Push(Strbuf)
                    Case "*/"
                        NewLine = True
                        Stack.Pop()
                    Case "//"
                        NewLine = False
                        sb.Remove(sb.Length - 1, 1)
                        sb.Append(ControlChars.NewLine & "/")
                End Select
                Select Case Strbuf
                    
                    Case "}", ";", ControlChars.NewLine, ControlChars.Lf, ControlChars.CrLf
                        NewLine = True
                End Select
            End If

            If NewLine = True Then
                sb.Append(Strbuf & ControlChars.NewLine)
            Else
                sb.Append(Strbuf)
            End If
            LastItem = Strbuf
                NewLine = False
        Next

        Dim TStr As String = sb.ToString
        TStr = TStr.Replace(ControlChars.NewLine & ControlChars.NewLine, ControlChars.NewLine)
        TStr = TStr.Replace(ControlChars.NewLine & ControlChars.NewLine, ControlChars.NewLine)
        Return TStr
    End Function
End Class
